<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('父级')" prop="parentId">
            <el-select
              v-model="form.parentId"
              :disabled="form.id !== undefined && form.id !== null && form.id !== ''"
              :placeholder="$t('父级任务')"
            >
              <el-option :label="$t('无')" value="0" />
              <el-option
                v-for="dict in parentOptions"
                :key="dict.id"
                :label="$t(dict.subjectName)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('科目代码')" prop="subjectCode">
            <el-input v-model="form.subjectCode" :placeholder="$t('科目代码')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('科目名称')" prop="subjectName">
            <el-input v-model="form.subjectName" :placeholder="$t('科目名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('汇总预算总金额')" prop="totalBudget">
            <Money v-model="form.totalBudget" :placeholder="$t('汇总预算总金额')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" min="0" max="999" oninput="value=value.replace(/[^\d.]/g,'')" maxlength="20" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fixedAssets:costBudget" />
    </div>
  </div>
</template>

<script>
import { saveDealCostBudget, getDealCostBudget, listDealCostBudget } from '@/api/deal/fixedAssets/dealCostBudget'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {},
      parentOptions: []
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data, parentId) {
      this.loading = true
      this.parentOptions = []
      if (data != null && data.id !== undefined) {
        getDealCostBudget(data.id).then(response => {
          this.form = response.data
          this.getParentSelect(this.form.dealId)
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        if (parentId != null && parentId !== undefined) {
            this.form.parentId = parentId
        }
        if (this.$route.query.id) {
          this.getParentSelect(this.$route.query.id)
        }
        this.loading = false
      }
    },
    getParentSelect(dealId) {
      listDealCostBudget({ dealId: dealId, parentId: '0' }).then(response => {
        this.parentOptions = response.rows
        console.log(this.parentOptions)
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        parentId: undefined,
        subjectCode: undefined,
        subjectName: undefined,
        totalBudget: undefined,
        sort: 0,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDealCostBudget(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
